import { login, logout } from '@/api/login.js'
import { adminIndexAuthList, adminUsersRead } from '@/api/common.js'

const user = {
    state: {
        userInfo: null, // 用户信息
        // 权限信息
        allAuth: null, // 总权限信息 默认空 调整动态路由
        crm: {} ,// 客户管理
        kms: {}, // 知识库管理
        psi: {}, // 进销存管理
    },

    mutations: {
        SET_USERINFO: (state, userInfo) => {
            state.userInfo = userInfo
            uni.setStorageSync('userInfo', userInfo);
        },
        SET_ALLAUTH: (state, allAuth) => {
            state.allAuth = allAuth
        },
        SET_CRM: (state, crm) => {
            state.crm = crm
        },
        SET_KMS: (state, kms) => {
            state.kms = kms
            uni.setStorageSync('kms', kms);
        },
        SET_PSI: (state, psi) => {
            state.psi = psi
            uni.setStorageSync('psi', psi);
        },
    },

    actions: {
        Login({
            commit
        }, userInfo) {
            const username = userInfo.username.trim()
            return new Promise((resolve, reject) => {
                login(username, userInfo.password,uni.getStorageSync("user_cid")).then(res => {
                    if (res['Admin-Token']) {
                        uni.setStorageSync('Admin-Token', res['Admin-Token']);
                        uni.setStorageSync('loginUserInfo', userInfo);
                        uni.setStorageSync('authList', res.auth);
                        commit('SET_USERINFO', res.user);
                        // 权限
                        commit('SET_ALLAUTH', res.auth);
                        commit('SET_CRM', res.auth.crm);
                        commit('SET_KMS', res.auth.kms);
                        commit('SET_PSI', res.auth.psi);
                    }
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取用户信息
        GetUserInfo({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                adminUsersRead().then(response => {
                    commit('SET_USERINFO', response.data)
                    resolve(response)
                }, function(error) {
                    uni.removeStorageSync('Admin-Token');
                    commit('SET_USERINFO', null);
                    uni.reLaunch({
                         url: '/pages/login/login'
                    });
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 登出
        LogOut({
            commit
        }) {
            return new Promise((resolve, reject) => {
                logout().then(() => {
                    try {
                        uni.removeStorageSync('Admin-Token');
                        uni.removeStorageSync('authList');
                        uni.removeStorageSync('psi');
                        uni.removeStorageSync('refreshData');
                        uni.removeStorageSync('userInfo');
                    } catch (e) {
                        reject(e)
                    }
                    resolve({
                        code: 200
                    })
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 获取权限
        getAuth({
            commit
        }) {
            return new Promise((resolve, reject) => {
                adminIndexAuthList().then((response) => {
                    const data = response.data
                    uni.setStorageSync('authList', data)
                    commit('SET_ALLAUTH', data)
                    commit('SET_CRM', data.crm)
					commit('SET_KMS', data.kms)
                    // commit('SET_BI', data.bi)
                    // commit('SET_MANAGE', data.manage)
                    // commit('SET_OA', data.oa)
                    // commit('SET_PROJECT', data.project)
                    commit('SET_PSI', data.psi);
                    // commit('SET_FINANCE', data.finance)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
    }
}

export default user
